// Created by Windward Studios - no copyright is claimed. This code can be used in
// any manner by anyone for any reason. There is no copyright of any kind on it. You may
// use it in commercial products. You may change it without sharing those changes.
// We ask that you keep the "created by Windward Studios" in a comment at the top.

using System.Collections;
using System.Data;
using System.Data.Common;
using Kailua.net.windward.utils;

namespace net.windward.utils.ado.PostgreSql
{
	/// <summary>
	/// An instance of a PostgreSQL server running on a computer.
	/// </summary>
	public class WrPostgreSqlServer : IWrServer
	{
		private readonly DbProviderFactory provider;
		private readonly string name;

		/// <summary>
		/// Create a PostgreSql server object.
		/// </summary>
		/// <param name="provider">The provider for this server.</param>
		/// <param name="name">The name of this server.</param>
		public WrPostgreSqlServer(DbProviderFactory provider, string name)
		{
			this.provider = provider;
			this.name = name;
		}

		#region IWrServer Members

		/// <summary>
		/// The name of the server.
		/// </summary>
		public string Name
		{
			get { return name; }
		}

		/// <summary>
		/// All databases on this server. If a provider does not have the concept of databases (Oracle)
		/// it will always return 1 database.
		/// </summary>
		/// <param name="credentials">The user credentials to access the database.</param>
		/// <returns>All databases on this server.</returns>
		public IWrDatabase[] GetDatabases(WrCredentials credentials)
		{
			ArrayList rtn = new ArrayList();
			using (IDbConnection sc = provider.CreateConnection())
			{
				sc.ConnectionString = WrPostgreSqlVendor.ConnectionString(provider, name, "postgres", credentials, true);
				sc.Open();
				using (IDbCommand cmd = sc.CreateCommand())
				{
					cmd.CommandType = CommandType.Text;
					cmd.CommandText = "select datname from pg_database";

					using (IDataReader myReader = cmd.ExecuteReader())
					{
						while (myReader.Read())
							rtn.Add(new WrPostgreSqlDatabase(provider, name, myReader.GetString(0)));
					}
				}
			}
			return (WrPostgreSqlDatabase[])rtn.ToArray(typeof(WrPostgreSqlDatabase));
		}

		/// <summary>
		/// All databases on this server. If a provider does not have the concept of databases (Oracle)
		/// it will always return 1 database.
		/// </summary>
		/// <param name="connStr">The connection string to access the database.</param>
		/// <returns>All databases on this server.</returns>
		public IWrDatabase[] GetDatabases(string connStr)
		{

			Trap.trap();
			ArrayList rtn = new ArrayList();
			using (IDbConnection sc = provider.CreateConnection())
			{
				sc.ConnectionString = connStr;
				sc.Open();
				using (IDbCommand cmd = sc.CreateCommand())
				{
					cmd.CommandType = CommandType.Text;
					/* what I got from the forums
					cmd.CommandText = "SELECT pg_database.datname as Database, pg_user.usename as Owner FROM pg_database, pg_user WHERE pg_database.datdba = pg_user.usesysid " +
						"UNION SELECT pg_database.datname as Database, NULL as Owner FROM pg_database WHERE pg_database.datdba NOT IN (SELECT usesysid FROM pg_user) " +
						"ORDER BY Database";
					*/
					cmd.CommandText = "SELECT d.datname as Name, r.rolname as Owner, pg_catalog.pg_encoding_to_char(d.encoding) as Encoding " +
										"FROM pg_catalog.pg_database d JOIN pg_catalog.pg_roles r ON d.datdba = r.oid ORDER BY 1";
					using (IDataReader myReader = cmd.ExecuteReader())
					{
						while (myReader.Read())
							rtn.Add(new WrPostgreSqlDatabase(provider, name, myReader.GetString(0)));
					}
				}
			}
			return (WrPostgreSqlDatabase[])rtn.ToArray(typeof(WrPostgreSqlDatabase));
		}

		#endregion

		/// <summary>
		/// Create a database object.
		/// </summary>
		/// <param name="database">The name of the database.</param>
		public IWrDatabase CreateDatabase(string database)
		{
			return new WrPostgreSqlDatabase(provider, name, database);
		}

		/// <summary>
		/// Compares the current instance with another object of the same type.
		/// </summary>
		/// <param name="obj">An object to compare with this instance. </param>
		/// <returns>a 32-bit signed integer that indicates the relative order of the objects being compared.</returns>
		public int CompareTo(object obj)
		{
			WrPostgreSqlServer db = obj as WrPostgreSqlServer;
			if (db == null)
				return 1;
			return name.CompareTo(db.name);
		}
	}
}
